package HotelRecommend;

import java.util.ArrayList;
import java.util.List;

import com.travel.core.AbstractDao;

public class TouristHotelRecommend {

	private HotelSearch hotelsearch;

	public TouristHotelRecommend() {
	}

	public TouristHotelRecommend(HotelSearch hotelsearch) {
		this.hotelsearch = hotelsearch;
	}

	public List<hotel> doTouristHotelRecommed() {
		List<String> feature = hotelsearch.getFeature();
		List<String> district = hotelsearch.getDistrict();
		List<String> star = hotelsearch.getStar();
		List<String> guize = new ArrayList<String>();
		String sort = hotelsearch.getSort();

        // SQL使用StringBuilder完成
		String fea = "";
		String dis = "";
		String sta = "";
		String search = null;
		if ((feature.size() == 0) || (feature.size() == 1 && feature.get(0).equals("不限"))) {
			fea = fea + "";
		} else {
			String s = "";
			for (String fe : feature) {
				s = s + "label like'%" + fe + "%' or ";
			}
			s = s.substring(0, s.lastIndexOf("or"));
			fea = s;
		}
		if ((district.size() == 0) || (district.size() == 1 && district.get(0).equals("不限"))) {
			dis = dis + "";
		} else {
			String d = "";
			for (String di : district) {
				d = d + "district like'%" + di + "%' or ";
			}
			d = d.substring(0, d.lastIndexOf("or"));
			dis = d;
		}
		if ((star.size() == 0) || (star.size() == 1 && star.get(0).equals("不限"))) {
			sta = sta + "";
		} else {
			String a = "";
			for (String st : star) {
				String[] stars = st.split("\\/");
				for (String s : stars) {
					a = a + "level like'%" + s + "%' or ";
				}
			}
			a = a.substring(0, a.lastIndexOf("or"));
			sta = a;
		}
		if (sort.equals("不限")) {
			search = "";
		}
		if (sort.equals("用户推荐")) {
			search = "order by recommend desc";
		}
		if (sort.equals("人气")) {
			search = "order by comment desc";
		}
		if (sort.equals("评分")) {
			search = "order by rating desc";
		}
		if (sort.equals("价格")) {
			search = "order by price asc";
		}
		guize.add(fea);
		guize.add(dis);
		guize.add(sta);
		guize.add(search);
		String sql = "select * from guilinhotel";
		if (fea.length() > 0) {
			sql = sql + " where (" + fea + ")";
			for (int i = 1; i < 3; i++) {
				if (guize.get(i).length() > 0) {
					sql = sql + "and (" + guize.get(i) + ")";
				}
			}
			sql = sql + " " + search;
		} else if (fea.length() == 0 && dis.length() > 0) {
			sql = sql + " where (" + dis + ")";
			if (sta.length() > 0) {
				sql = sql + "and (" + sta + ") " + search;
			}
		} else if (fea.length() == 0 && dis.length() == 0 && sta.length() > 0) {
			sql = sql + " where (" + sta + ") " + search;
		} else if (fea.length() == 0 && dis.length() == 0 && sta.length() == 0 && search.length() > 0) {
			sql = sql + " " + search;
		}
		System.out.println(sql);
		return AbstractDao.getHotel(sql);
	}

	public static void main(String[] args) {
		List<String> feature = new ArrayList<String>();
		feature.add("休闲度假");
		feature.add("亲子时刻");
		List<String> district = new ArrayList<String>();
		district.add("火车南站");
		district.add("会展中心");
		List<String> star = new ArrayList<String>();
		star.add("三星/舒适");
		String sort = "用户推荐";
		HotelSearch hotelsearch = new HotelSearch(feature, district, star, sort);
		List<hotel> hotels = new TouristHotelRecommend(hotelsearch).doTouristHotelRecommed();
		for (hotel h : hotels) {
			System.out.println(h.getHotelname() + "  " + h.getDistrict() + "  " + h.getLevel() + "  " + h.getLabel());
		}
	}
}
